package com.cash.loan.modular.cash.annotation.support;

import com.cash.loan.modular.cash.service.StrategyService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 * @Date 2018-07-20 16:26
 */
@Component
@Aspect
public class ScanSupport {

    private static Logger logger = LoggerFactory.getLogger(PvUvSupport.class);

    @Autowired
    private StrategyService strategyService;

    @Pointcut("@annotation(com.cash.loan.modular.cash.annotation.ScanLog)")
    private void scanLog() {
    }

    @Around("scanLog()")
    public Object statisticsPvAndUv(ProceedingJoinPoint point) throws Throwable {
        //先执行业务
        Object result = point.proceed();
        //执行统计
        try {
            handle(point);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("统计点击量出错",e.getMessage());
        }
        return result;
    }

    /**
     * 统计功能实现
     * @param point
     * @throws Exception
     */
    private void handle(ProceedingJoinPoint point) throws Exception {
        Object[] args = point.getArgs();

        Long strategyId = null;


        if(args[0] != null) {
            strategyId = (Long)args[0];
        }

        strategyService.strategyScan(strategyId);
    }
}
